package com.dyz.leetcode;

public class Rotate48 {
    //解题一
    public void rotate(int[][] matrix) {
        int n = matrix.length;
        for(int i=0; i<(n+1)/2;i++){
            for(int j=0;j<(n+1)/2;j++){
                int temp = matrix[i][j];
                matrix[i][j] = matrix[n-1-j][i];
                matrix[n-1-j][i] = matrix[n-1-i][n-1-j];
                matrix[n-1-i][n-1-j]=matrix[j][n-1-i];
                matrix[j][n-1-i] = temp;
            }
        }
    }
    
    //解题二
    public void rotate1(int[][] matrix){
        int n = matrix.length;
        if(n<=1){return;}
        for (int r=0;r<n;r++){
            int s = r;
            int e = n-r-1;
            for(int i=s; i<e; i++){
                int x = r;
                int y = i;
                swap(matrix, x, y, y, e);
                swap(matrix, x, y, e, e-(y-s));
                swap(matrix, x, y, e-(y-s), s);
            }
        }
        
    }

    private void swap(int[][] matrix, int x1, int y1, int x2, int y2) {
        int tmp = matrix[x1][y1];
        matrix[x1][y1] = matrix[x2][y2];
        matrix[x2][y2] = tmp;
    }
}
